from django.db import models
from django.contrib.auth.models import User


class Profile(models.Model):
    # 与user模型进行一对一管理
    user = models.OneToOneField(User, on_delete=models.CASCADE, verbose_name='用户')
    # 自定义一个字段
    nickname = models.CharField(max_length=20, verbose_name='昵称')
    # 对用户模型进行拓展

    def __str__(self):
        return '<Profile: %s for %s>' % (self.nickname, self.user.username)


# 获取昵称
def get_nickname(self):
    if Profile.objects.filter(user=self).exists():
        profile = Profile.objects.get(user=self)
        return profile.nickname
    else:
        return ''


def get_nickname_or_username(self):
    if Profile.objects.filter(user=self).exists():
        profile = Profile.objects.get(user=self)
        return profile.nickname
    else:
        return self.username


def has_nickname(self):
    return Profile.objects.filter(user=self).exists()


# 动态绑定
User.get_nickname = get_nickname
User.get_nickname_or_username = get_nickname_or_username
User.has_nickname = has_nickname
